
/*
 *
 * gcc -o example2 example2.c ../src/log.c
 * 
 * https://zhuanlan.zhihu.com/p/514469654
 *
 */

#include <pthread.h>
#include "../src/log.h"

pthread_mutex_t MUTEX_LOG;
void log_lock(bool lock, void *udata);

int main()
{
    log_set_level(0);
    log_set_quiet(0);

    pthread_mutex_init(&MUTEX_LOG, NULL);
    log_set_lock(log_lock, &MUTEX_LOG);

    /* Insert threaded application code here... */
    log_info("I'm threadsafe");

    pthread_mutex_destroy(&MUTEX_LOG);

    return 0;
}

void log_lock(bool lock, void* udata)
{
    pthread_mutex_t *LOCK = (pthread_mutex_t*)(udata);
    if (lock)
        pthread_mutex_lock(LOCK);
    else
        pthread_mutex_unlock(LOCK);
}
